# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Node:
    def __init__(self, val=None, next=None):
        self.val = val
        self.next = next


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def insert(self, head: 'Node', insertVal: int) -> 'Node':
        insert_node = Node(insertVal)
        node = head

        if node is None:
            insert_node.next = insert_node
            return insert_node
        elif node.next == node:
            node.next = insert_node
            insert_node.next = node
        else:
            prev_node = None
            tail_node = None
            while node is not None:
                if node.val == insertVal:
                    temp_node = node.next
                    node.next = insert_node
                    insert_node.next = temp_node
                    break
                elif node.val > insertVal:
                    if node.val > node.next.val:
                        if insertVal < node.next.val:
                            temp_node = node.next
                            node.next = insert_node
                            insert_node.next = temp_node
                            break
                    if prev_node is not None:
                        prev_node.next = insert_node
                        insert_node.next = node
                        break
                elif node.val < insertVal:
                    if not prev_node:
                        prev_node = node
                    elif node.val >= prev_node.val:
                        prev_node = node

                if node.next == head:
                    tail_node = node

                node = node.next

                if node == head:
                    if not prev_node:
                        prev_node = tail_node

                    temp_node = prev_node.next
                    prev_node.next = insert_node
                    insert_node.next = temp_node
                    break

        return head


def array_to_node(values: List[int]) -> Node:
    if len(values) == 0:
        return None

    head = Node(values[0])
    prev_node = head
    for i in range(1, len(values)):
        node = Node(values[i])
        prev_node.next = node
        prev_node = node
    prev_node.next = head
    return head


def node_to_array(node: Node) -> List[int]:
    ret_val = []
    head = node

    while node is not None:
        ret_val.append(node.val)
        node = node.next

        if node == head:
            break

    return ret_val


if __name__ == '__main__':
    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ([3, 5, 1], 0, [3, 5, 0, 1]),
        ([3, 4, 1], 2, [3, 4, 1, 2]),
        ([], 1, [1]),
        ([1], 0, [1, 0])
    ]

    for case_item in testcases:
        solution = Solution()
        # 获取测试用的case和期待的执行结果
        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]

        exec_result = node_to_array(solution.insert(array_to_node(input1), input2))

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

